/**
 * File Name   : RandomFileRenamePolicy.java
 * Description : 
 */
package com.hh.common.util.upload;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 文件上传，自定义命名策略
 * @author 冯昌硕
 * @create 2013-1-26 上午10:41:14
 */
public class RandomFileRenamePolicy implements IFileRenamePolicy {

    private static final Logger log = Logger.getLogger(RandomFileRenamePolicy.class);
    
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    
    /**
     * 获取一个新的文件名
     * @return
     * @create 2013-1-27 上午12:23:46
     */
    public String rename(String fileName) {
        
        String dateStr = sdf.format(new Date());
        
        String newFileName = null;
        
        int potIdx = fileName.lastIndexOf(".");
        if(potIdx != -1) {
            newFileName = dateStr + "_" + System.currentTimeMillis() + fileName.substring(potIdx);
        } else {
            newFileName = dateStr + "_" + System.currentTimeMillis();
        }
        
        log.debug("上传文件[" + fileName + "]重命名后为[" + newFileName + "]");
        return newFileName;
    }

}
